﻿using MediatR;
using RBAC.Domain.WarehouseManagement;
using RBAC.ErrorCode;
using RBAC.Interstructrue.Base;
using RBAC.Write.Api.Application.Command.ERP.WarehouseManagement;

namespace RBAC.Write.Api.Application.Handler.ERP.WarehouseManagement
{
    public class AddWarehouseListCommandHandler : IRequestHandler<AddWarehouseListCommands, APIResult<int>>
    {
        private readonly IBaseRepository<WarehouseList> warehouseListRep;

        public AddWarehouseListCommandHandler(IBaseRepository<WarehouseList> warehouseListRep)
        {
            this.warehouseListRep = warehouseListRep;
        }

        public async Task<APIResult<int>> Handle(AddWarehouseListCommands request, CancellationToken cancellationToken)
        {
            try
            {
                // 参数验证
                if (request.warehouseList == null)
                {
                    return new APIResult<int>
                    {
                        Code = APIEnums.Fail,
                        Msg = "仓库信息不能为空",
                        Data = 0
                    };
                }

                // 验证必填字段
                if (string.IsNullOrWhiteSpace(request.warehouseList.WarehouseCode))
                {
                    return new APIResult<int>
                    {
                        Code = APIEnums.Fail,
                        Msg = "仓库代码不能为空",
                        Data = 0
                    };
                }

                if (string.IsNullOrWhiteSpace(request.warehouseList.WarehouseName))
                {
                    return new APIResult<int>
                    {
                        Code = APIEnums.Fail,
                        Msg = "仓库名称不能为空",
                        Data = 0
                    };
                }

                // 检查仓库代码是否已存在
                var existingWarehouse = warehouseListRep.GetFirstOrDefault("WarehouseCode = @WarehouseCode", new { WarehouseCode = request.warehouseList.WarehouseCode });
                if (existingWarehouse != null)
                {
                    return new APIResult<int>
                    {
                        Code = APIEnums.Fail,
                        Msg = "仓库代码已存在，请使用其他代码",
                        Data = 0
                    };
                }

                // 设置默认值
                request.warehouseList.CreateTime = DateTime.Now;
                request.warehouseList.CreateUser = "system";
                request.warehouseList.IsDeleted = false;

                // 使用Dapper进行新增操作
                var result = warehouseListRep.Create(request.warehouseList);

                if (result > 0)
                {
                    return new APIResult<int>
                    {
                        Code = APIEnums.Success,
                        Msg = "仓库添加成功",
                        Data = result
                    };
                }
                else
                {
                    return new APIResult<int>
                    {
                        Code = APIEnums.Fail,
                        Msg = "仓库添加失败",
                        Data = 0
                    };
                }
            }
            catch (Exception ex)
            {
                return new APIResult<int>
                {
                    Code = APIEnums.Fail,
                    Msg = "添加仓库时发生错误：" + ex.Message,
                    Data = 0
                };
            }
        }
    }
}
